//
//  SWPackageHeaderView.swift
//  ChildStory
//
//  Created by linhan on 2017/1/14.
//  Copyright © 2017年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWPackageHeaderView: UIView
{
    weak var delegate:ClickActionDelegate?
    
    var selectedIndex:Int = 0{
        didSet{
            _roleGroup.selectedIndex = selectedIndex
            if let package = packages.valueAt(selectedIndex)
            {
                updateContentView(package: package)
            }
        }
    }
    
    var packages:[SWProductPackage] = []{
        didSet{
            updateSelectionView()
        }
    }
    
    private var _roleGroup:SWRadioButtonGroup = SWRadioButtonGroup()
    private var _selectionView:UIView = UIView()
    private var _contentLabel:UILabel = UILabel()
    
    private class RadioButton:UIButton,SWIRadioButton
    {
        var beSelected:Bool{
            get{
                return isEnabled
            }
            set{
                isEnabled = !newValue
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup()
    {
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        let detailBtn:UIButton = UIButton(type: .system)
        detailBtn.setTitle("详情>", for: .normal)
        detailBtn.sizeToFit()
        detailBtn.addTarget(self, action: #selector(detailBtnTapped), for: .touchUpInside)
        detailBtn.center = CGPointMake(width * 0.5, 20)
        addSubview(detailBtn)
        
        _selectionView.frame = CGRectMake(0, detailBtn.bottom, width, 40)
        _selectionView.backgroundColor = UIColor.white
        addSubview(_selectionView)
        
        _contentLabel.frame = CGRectMake(0, _selectionView.bottom, width, height - _selectionView.bottom)
        _contentLabel.numberOfLines = 0
        addSubview(_contentLabel)
    }
    
    private func updateSelectionView()
    {
        _roleGroup.removeAll()
        _selectionView.removeAllSubview()
        
        var items:[RadioButton] = []
        for (i, package) in packages.enumerated()
        {
            let view:RadioButton = createRadioButton(title: package.title)
            view.tag = i
            view.addTarget(self, action: #selector(roleBtnTapped(_:)), for: .touchUpInside)
            items.append(view)
            _selectionView.addSubview(view)
        }
        _roleGroup.addItems(items)
        LayoutUtil.distributeHAverageABS(items, containerWidth: _selectionView.width)
        
        let index = selectedIndex
        selectedIndex = index
    }
    
    private func updateContentView(package:SWProductPackage)
    {
        _contentLabel.text = package.title
    }
    
    private func createRadioButton(title:String) -> RadioButton
    {
        let margin:CGFloat = 15
        let button:RadioButton = RadioButton(type:.custom)
        button.setTitle(title, for: .normal)
        button.setTitle(title, for: .disabled)
        //button.titleEdgeInsets = UIEdgeInsetsMake(0, margin, 0, 0)
        button.setTitleColor(UIColor(hex:0x2f3231), for: .normal)
        button.setImage(UIImage(named: "child_radio_normal"), for: .normal)
        button.setImage(UIImage(named: "child_radio_selected"), for: .disabled)
        button.sizeToFit()
        button.frame = CGRectMake(0, 0, button.width + margin, button.height)
        return button
    }
    
    @objc private func roleBtnTapped(_ button:UIButton)
    {
        if let button = button as? SWIRadioButton
        {
            _roleGroup.selectItem(button)
            if let tag = (button as? UIView)?.tag
            {
                //_roleGroup.selectedIndex = tag
                delegate?.view(self, click: CallbackAction.Select, argument: [SWKey.index:tag])
            }
        }
    }
    
    @objc private func detailBtnTapped()
    {
        delegate?.view(self, click: CallbackAction.Detail, argument: nil)
    }
    
    
}
